package com.czp.person.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;

import com.czp.framework.model.DataStoreTemplate;
import com.czp.framework.util.DateUtils;
import com.czp.person.mapper.IPersonMapper;
import com.czp.person.model.Person;
import com.czp.person.service.IPersonService;
import com.czp.user.model.User;
import com.google.gson.Gson;

/**
 * 【环 境】：J2SE 1.7
 * @author   柴志鹏	CHAIZP@GMAIL.COM
 * @version  version 1.0
 * @since    2014-6-24
 */
public class PersonServiceImpl implements IPersonService {
	private IPersonMapper personMapper;
	private Gson gson = new Gson();
	
	public String getPersonList(Person person) throws Exception {
		DataStoreTemplate dst = new DataStoreTemplate();
		dst.setList(personMapper.getPersonList(person, new RowBounds(person.getStart(), person.getLimit())));
		dst.setTotal(personMapper.getPersonRecords(person));
		return gson.toJson(dst);
	}
	
	public int personController(Person person, User userId) throws Exception {
		person.setModifyDate(DateUtils.getSysTimestamp());
		if(person.getCudType().equals("C")) {//添加
			person.setCreateDate(DateUtils.getSysTimestamp());
			return personMapper.insertPerson(person);
		} else if(person.getCudType().equals("U") && !person.getId().equals("")) {//修改
			return personMapper.updatePerson(person);
		} else if(person.getCudType().equals("D") && !person.getId().equals("")) {//删除
			return personMapper.deletePerson(person, userId);
		} else {
			return 0;
		}
	}
	
	public String findPersonById(String personId) throws Exception {
		return gson.toJson(
				personMapper.findPersonById(personId));
	}
	
	public String getPersonName(String name) throws Exception {
		List<Person> list = personMapper.getPersonName(name);
		int size = list.size();
		String jobStr[][] = new String[size][2];
		for(int i = 0; i < size; i++) {
			jobStr[i][0] = list.get(i).getId();
			jobStr[i][1] = list.get(i).getPersonName();
		}
		return gson.toJson(list);
	}
	
	/**
	 * 获取 personMapper
	 * @return 返回 personMapper
	 */
	public IPersonMapper getPersonMapper() {
		return personMapper;
	}

	/**
	 * 设置 personMapper
	 * @param 对personMapper进行赋值
	 */
	public void setPersonMapper(IPersonMapper personMapper) {
		this.personMapper = personMapper;
	}
}
